function numFormat(value: number): string {
    return value > 9 ? value + '' : '0' + value;
}
function timeFormat(date: number): string {
    if (!date || +date === 0) {
        return '--';
    }
    const dateFormat = new Date(date * 1000);
    const yy = dateFormat.getFullYear();
    const mm = numFormat(dateFormat.getMonth() + 1);
    const dd = numFormat(dateFormat.getDate());
    const hh = numFormat(dateFormat.getHours());
    const min = numFormat(dateFormat.getMinutes());
    const ss = numFormat(dateFormat.getSeconds());
    return yy + '.' + mm + '.' + dd + ' ' + hh + ':' + min + ':' + ss;
}
function objFormat(obj: any): any {
    const pro = [
        'stime',
        'utime',
        'etime',
        'publish_time',
        'unpublish_time',
        'ctime'
    ];
    pro.forEach((item: string) => {
        obj[item] && (obj[item] = timeFormat(obj[item]));
    });
    return obj;
}
function dataFormat(data: any) :any {
    if(Array.isArray(data)){
        return data.map(item =>{
            if (typeof item === 'object') {
                return objFormat(item);
            } else {
                return item;
            }
        })
    }else{
        const pro = ['stime', 'etime', 'publish_time', 'unpublish_time'];
        pro.forEach((item: string) => {
            data[item] && (data[item] = Math.round(data[item] / 1000) + '');
        });
        return data;  
    }
}
function dateFormat(data: any): any {
    if (Array.isArray(data)) {
        return data.map(item => {
            if (typeof item === 'object') {
                return objFormat(item);
            } else {
                return item;
            }
        });
    } else {
        const pro = ['stime', 'etime', 'publish_time', 'unpublish_time'];
        pro.forEach((item: string) => {
            data[item] && (data[item] = data[item] * 1000);
        });
        return data;
    }
}
function urlFormat(path: string): string {
    return path
        ? process.env.VUE_APP_IMA + path + '?v=' + process.env.VUE_APP_API_V
        : '';
}
async function downloadRes(url: string, name: string) {
    const response = await fetch(urlFormat(url)); // 内容转变成blob地址
    const blob = await response.blob(); // 创建隐藏的可下载链接
    const objectUrl = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = objectUrl;
    a.download = name + '.xls';
    a.click();
    a.remove();
}
function statusFormat(st: string, et: string): number {
    const now = new Date();
    return now < new Date(st) ? 1 : now > new Date(et) ? 3 : 2;
}
export default app => {
    app.config.globalProperties.$utils = {
        timeFormat,
        dateFormat,
        dataFormat,
        urlFormat,
        downloadRes,
        statusFormat
    };
};